package com.shujia.scala

import scala.collection.mutable
import scala.collection.mutable.ListBuffer

object Demo24mutable {
  def main(args: Array[String]): Unit = {
    //1、可变list
    val list = new ListBuffer[Int]

    //增加元素
    list += 1
    list += 2
    list += 1

    println(list)

    //删除元素
    list -= 1

    println(list)

    //修改元素
    list(0) = 100
    println(list)

    //批流增加元素
    list ++= List(2, 2, 2, 2, 2)

    println(list)

    //转换成不可变列表
    val list1: List[Int] = list.toList
    println(list1)

    //2、可变Map
    val map = new mutable.HashMap[String, String]
    //增加元素
    map.put("name", "张三")
    println(map)

    //map.+=(("sex", "男"))
    map += "sex" -> "男"
    println(map)

    //删除元素
    map -= "name"
    map.remove("sex")
    println(map)

    //修改
    map += "sex" -> "男"
    map += "sex" -> "女"
    println(map)

    //批量增加
    map ++= Map("1" -> "2", "3" -> "4")
    println(map)

    //3、可变Set
    val set = new mutable.HashSet[String]()

    set += "java"
    set += "hadoop"
    println(set)

    set -= "java"
    println(set)

    set ++= Set("1", "2", "3")
    println(set)
  }
}